<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/import.jsp"%>
<%
    if ("login".equals(act)) {
        //用户登录
        String phone = input.getDesc("user_name", "");
        String password = input.getDesc("password", "");
        password = Tools.getMD5(password);
        List<User> list = QBuilder.db("basedb").select().from("User").where(new Where("phone=", phone).and("type=", 200).and("password=", password).and("hospital=", 999).and("status=1")).query().getResultObj();
        if (list.size() == 0) {
            msg.setErrcodeAndErrmsg(30021, "用户名或密码错误");
            Tools.print(out, msg.toJson());
        } else {
            User user = list.get(0);
            msg.setErrmsg("登录成功");
            session.setAttribute("roles", getPrivsByUser(user));
            session.setAttribute("menus", getMenusByUser(user));
            session.setAttribute("user", user);
            session.setAttribute("hospital", user.getHospital());
            List<Map> roles = QBuilder.db("basedb").select("new map(r.name as name, r.id as id)").from("UserRole as ur, Role as r").where(new Where("ur.roleId=r.id").and("ur.userId=", user.getId()).and("ur.type=", COMMON_USER_TYPE)).query().getResultObj();
            session.setAttribute("user_role", isEmpty(roles)?"未知角色":roles.get(0).get("name"));
            session.setAttribute("user_role_id", isEmpty(roles)?-1:roles.get(0).get("id"));
            Tools.print(out, msg.toJson());
        }
        return;
    }
%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link rel="stylesheet" href="../css/txzj/login.css?cache=<%=Math.random()%>">
    <link rel="stylesheet" href="/css/sweetalert.css">
</head>
<body>
<div class="header"></div>
<div class="main">
    <div class="login">
        <div>
            <input type="text" placeholder="请输入用户名" class="user-message" id="login_name">
            <input type="password" placeholder="请输入密码" class="user-message" id="password">
            <div class="login-style">
                <input type="checkbox" class="checkbox-style" id="remember" >
                <span class="rem_pass">记住密码</span>
            </div>
            <div class="sure_login"  id="submit">登录</div>
        </div>
    </div>
</div>
<div class="footer">
    <div>
        <div>版权所有</div>
        <div><a href="http://www.tianxiabuyi.com" target="_blank">苏州天下布医信息科技有限公司</a></div>
    </div>
</div>
<script src="/js/jquery-1.9.1.min.js"></script>
<script src="/js/jquery.cookie.js"></script>
<script src="/js/sweetalert-dev.js"></script>
<script>
    var code = document.getElementById('code');
    var submit = document.getElementById('submit');
    function loginVali() {
        submit.onclick = finalVali;
        document.body.addEventListener("keyup", function() {
            if (window.event.keyCode == 13) {
                finalVali();
            }
        })
        function finalVali() {
            if($('#login_name').val().trim() == "") {
                swal("请输入用户名");
                return;
            } else if($('#password').val() == "") {
                swal("请输入密码");
                return false;
            } else {
                //存入的时候密码以用户名为salt
                var password = $('#password').val();
                save();
                $.ajax({
                    type: 'post',
                    url: 'login.jsp',
                    data:{
                        user_name:$('#login_name').val(),
                        password:password,
                        act:"login"
                    } ,
                    dataType: 'json',
                    success: function (result) {
                        if (result.errcode!=0) {
                            swal("用户名或者密码错误");
                        }
                        else {
                            location.href = "/index.jsp";
                        }
                    } ,
                    error: function (error) {
                        swal("用户名或者密码错误");
                    }
                });
            }
        }
    }
    loginVali();
    //记住用户名密码
    function save() {
        if ($("#remember").prop("checked")) {
            var username = $("#login_name").val();
            var password = $("#password").val();
            $.cookie("rmbUser", "true", { expires: 7 } ); //存储一个带7天期限的cookie
            $.cookie("username", username, { expires: 7 } );
            $.cookie("password", password, { expires: 7 } );
        } else{
            $.cookie("rmbUser", "false", { expire: -1 } );
            $.cookie("username", "", { expires: -1 } );
            $.cookie("password", "", { expires: -1 } );
        }
    } ;
    $(document).ready(function() {
        if ($.cookie("rmbUser") == "true") {
            $("#remember").prop("checked", true);
            $("#login_name").val($.cookie("username"));
            $("#password").val($.cookie("password"));
        }
    } );
</script>
</body>
</html>

